package com.easyfun.healthmagicbox.widget;

import java.util.ArrayList;

import com.easyfun.healthmagicbox.pojo.ConstantData;

import junit.framework.Assert;
import com.easyfun.healthmagicbox.R;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.widget.EditText;
import android.widget.Toast;


public class CustomDialogUtil {
	Context mContext = null;
	ArrayList<String> items = new ArrayList<String>();
	ArrayList<String> values = new ArrayList<String>();
	ArrayList<Boolean> selected = new ArrayList<Boolean>();
	String itemsInStr = "";
	String valuesInStr = "";
	String dialogTitle = "";
	EditText edit;
	Dialog dialog = null;
	boolean mMultiChoiceFlag = true;
	
	public CustomDialogUtil(Context context, ArrayList<String> items,EditText edit) {
		mContext = context;
		this.items = items;
		this.values = items;
		
		initData();
		this.edit = edit;
	}
	
	public CustomDialogUtil(Context context, ArrayList<String> items,String dialogTitle) {
		
		mContext = context;
		this.items = items;
		this.values = items;
		this.edit = null;
		
		initData();
		this.dialogTitle = dialogTitle;
	}
	public CustomDialogUtil(Context context, ArrayList<String> items,EditText edit, String dialogTitle) {
		mContext = context;
		this.items = items;
		this.values = items;	
		this.edit = edit;
		
		initData();
		this.dialogTitle = dialogTitle;
	}
	
	public CustomDialogUtil(Context context, ArrayList<String> items, 
			ArrayList<String> values, EditText edit) 
	{
		mContext = context;
		Assert.assertEquals(items.size(), values.size());
		this.items = items;
		this.values = values;
		this.edit = edit;

		initData();
	}


	private void initData() {
		for (int i = 0; i < items.size(); i++) {
			selected.add(i, false);			
		}	
		this.dialogTitle = mContext.getString(R.string.custom_dialog_default_title);
	}
	
	public void createDialog(boolean multiChoiceFlag) {
		this.mMultiChoiceFlag = multiChoiceFlag;
		createDialog();
		
	}
	
	public void createDialog() {
		if (items.size() == 0) {
			Toast.makeText(mContext, "您还没有健康数据呀！",
					Toast.LENGTH_SHORT).show();
			return;
		}
		
		String[] typenames = new String[items.size()];
		for (int i = 0; i < items.size(); i++) {
			typenames[i] = items.get(i);
		}
		Intent personProfileDataIntent = new Intent(mContext, CustomDialogTemplate.class);
		personProfileDataIntent.putExtra(ConstantData.CUSTOM_DIALOG_MULIT_CHOICE_FLAG, mMultiChoiceFlag);
		personProfileDataIntent.putExtra(ConstantData.CUSTOM_DIALOG_INPUT_DATA, typenames);
		personProfileDataIntent.putExtra(ConstantData.CUSTOM_DIALOG_TITLE, this.dialogTitle);
		 
		((Activity)mContext).startActivityForResult(personProfileDataIntent, ConstantData.CUSTOM_DIALOG_REQUST_ID);
		return;
	}
	

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		  switch (requestCode) {
	        case ConstantData.CUSTOM_DIALOG_REQUST_ID:
	            // When user selects one with success
	            if (resultCode == Activity.RESULT_OK) {
	                // draw Grid View
	            	selected = (ArrayList<Boolean>)(data.getExtras().getSerializable(ConstantData.CUSTOM_DIALOG_RETURN_DATA));
	            	itemsInStr = "";
	            	valuesInStr = "";
	            	for(int i = 0; i < selected.size(); i ++) {
	            		if(selected.get(i)) {
	            			itemsInStr = itemsInStr + items.get(i) + ",";
    						valuesInStr = valuesInStr + values.get(i) + ",";
	            		}
	            	}
	            	itemsInStr = removeLastComma(itemsInStr);
    				valuesInStr = removeLastComma(valuesInStr);
    				edit.setText(itemsInStr);
	            }
	            break;
	        }
		return;
	}
	
	public ArrayList<String> getItems() {
		return this.items;
	}
	
	public void setItems(ArrayList<String> items) {
		this.items = items;
	}

	public String getValuesInStr() {
		return valuesInStr;
	}
	public String getItemsInStr() {
		return itemsInStr;
	}

	public void setValuesInStr(String ivaluesInStr) {
		valuesInStr = ivaluesInStr;
		String[] valueitems = ivaluesInStr.split(",");
		for(String value:valueitems)
		{
			if(value.length() == 0) continue;
			for (int i=0;i<values.size();i++)
			{
				if (value.equals(values.get(i)))
					selected.set(i, true);
			}
		}
		for (int i = 0; i < items.size(); i++) {
			if (selected.get(i))
			{
				itemsInStr = itemsInStr + items.get(i) + ",";				
			}			
		}
		itemsInStr = removeLastComma(itemsInStr);
	}
	
	private String removeLastComma(String str)
	{
		if (str.length() == 0)
			return str;
		if (str.charAt(str.length()-1) == ',')
		{
			return str.substring(0,str.length()-1);
		}
		return str;
	}
}
